﻿using System;

namespace RotovapPlatform.Domain.Equipment.Device.IO
{
	/// <summary>
	/// 数字输入信号ID
	/// </summary>
	public enum DISignalId
	{
		// DI1模块
		/// <summary>
		/// 蒸发瓶液位传感器
		/// </summary>
		EvaporateFlaskLevelSensor = 0,  // DI1-1

		/// <summary>
		/// 推杆伸出限位传感器
		/// </summary>
		PushRodExtendLimitSensor = 1,  // DI1-2

		/// <summary>
		/// 推杆缩回限位传感器
		/// </summary>
		PushRodRetractLimitSensor = 2,  // DI1-3

		/// <summary>
		/// 冷凝器液位传感器
		/// </summary>
		CondenserLevelSensor = 3,  // DI1-4

		/// <summary>
		/// 补油输液液位传感器1
		/// </summary>
		OilReplenishLevelSensor1 = 4,  // DI1-5

		/// <summary>
		/// 补油输液液位传感器2
		/// </summary>
		OilReplenishLevelSensor2 = 5,  // DI1-6

		/// <summary>
		/// 废液瓶液位传感器
		/// </summary>
		WasteBottleLevelSensor = 6,  // DI1-7

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI1_8 = 7,  // DI1-8

		// DI2模块
		/// <summary>
		/// 急停
		/// </summary>
		EmergencyStop = 8,  // DI2-1

		/// <summary>
		/// 复位
		/// </summary>
		Reset = 9,  // DI2-2

		/// <summary>
		/// 门磁开关1
		/// </summary>
		DoorSwitch1 = 10,  // DI2-3

		/// <summary>
		/// 门磁开关2
		/// </summary>
		DoorSwitch2 = 11,  // DI2-4

		/// <summary>
		/// 门磁开关3
		/// </summary>
		DoorSwitch3 = 12,  // DI2-5

		/// <summary>
		/// 门磁开关4
		/// </summary>
		DoorSwitch4 = 13,  // DI2-6

		/// <summary>
		/// 门磁开关5
		/// </summary>
		DoorSwitch5 = 14,  // DI2-7

		/// <summary>
		/// 门磁开关6
		/// </summary>
		DoorSwitch6 = 15,  // DI2-8

		// DI3模块
		/// <summary>
		/// 关爪限位开关1
		/// </summary>
		GripperLimitSwitch1 = 16,  // DI3-1

		/// <summary>
		/// 关爪限位开关2
		/// </summary>
		GripperLimitSwitch2 = 17,  // DI3-2

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI3_3 = 18,  // DI3-3

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI3_4 = 19,  // DI3-4

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI3_5 = 20,  // DI3-5

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI3_6 = 21,  // DI3-6

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI3_7 = 22,  // DI3-7

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI3_8 = 23,  // DI3-8

		// DI4模块（保留，全部定义为备用）
		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_1 = 24,  // DI4-1

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_2 = 25,  // DI4-2

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_3 = 26,  // DI4-3

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_4 = 27,  // DI4-4

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_5 = 28,  // DI4-5

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_6 = 29,  // DI4-6

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_7 = 30,  // DI4-7

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DI4_8 = 31   // DI4-8
	}

	/// <summary>
	/// 数字输出信号ID
	/// </summary>
	public enum DOSignalId
	{
		// DO1模块
		/// <summary>
		/// 冷凝管加液两通阀门1
		/// </summary>
		CondenserAddLiquidValve1 = 0,  // DO1-1

		/// <summary>
		/// 冷凝管加液两通阀门2
		/// </summary>
		CondenserAddLiquidValve2 = 1,  // DO1-2

		/// <summary>
		/// 冷凝管洗液两通阀门
		/// </summary>
		CondenserWashLiquidValve = 2,  // DO1-3

		/// <summary>
		/// 蒸发瓶清洗两通阀门
		/// </summary>
		EvaporateFlaskCleanValve = 3,  // DO1-4

		/// <summary>
		/// 排冷凝液两通阀门
		/// </summary>
		ExhaustCondensateLiquidValve = 4,  // DO1-5

		/// <summary>
		/// 冷凝液回收两通阀门
		/// </summary>
		CondensateRecycleValve = 5,  // DO1-6

		/// <summary>
		/// 冷凝液回收三通阀门
		/// </summary>
		CondensateRecycleThreeWayValve = 6,  // DO1-7

		/// <summary>
		/// 进液台两通阀1
		/// </summary>
		FeedingLiquidValve1 = 7,  // DO1-8

		// DO2模块
		/// <summary>
		/// 进液台两通阀2
		/// </summary>
		FeedingLiquidValve2 = 8,  // DO2-1

		/// <summary>
		/// 取液两通阀
		/// </summary>
		ExtractionLiquidValve = 9,  // DO2-2

		/// <summary>
		/// 洗液桶三通阀
		/// </summary>
		WashingLiquidBucketThreeWayValve = 10,  // DO2-3

		/// <summary>
		/// 清洗三通阀1
		/// </summary>
		WashingThreeWayValve1 = 11,  // DO2-4

		/// <summary>
		/// 清洗三通阀2
		/// </summary>
		WashingThreeWayValve2 = 12,  // DO2-5

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DO2_6 = 13,  // DO2-6

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DO2_7 = 14,  // DO2-7

		/// <summary>
		/// 备用
		/// </summary>
		Reserved_DO2_8 = 15,  // DO2-8

		// DO3模块
		/// <summary>
		/// 推杆伸缩控制方向
		/// </summary>
		PushRodExtendDirection = 16,  // DO3-1

		/// <summary>
		/// 日光灯开关/双路继电器
		/// </summary>
		DaylightLampSwitch = 17,  // DO3-2

		/// <summary>
		/// 真空泵启停控制/双路继电器
		/// </summary>
		VacuumPumpControl = 18,  // DO3-3

		/// <summary>
		/// 备用/双路继电器
		/// </summary>
		Reserved_DO3_4 = 19,  // DO3-4

		/// <summary>
		/// 备用/双路继电器
		/// </summary>
		Reserved_DO3_5 = 20,  // DO3-5

		/// <summary>
		/// 三色灯-红/单路继电器
		/// </summary>
		TriColorLamp_Red = 21,  // DO3-6

		/// <summary>
		/// 三色灯-黄/单路继电器
		/// </summary>
		TriColorLamp_Yellow = 22,  // DO3-7

		/// <summary>
		/// 三色灯-绿/单路继电器
		/// </summary>
		TriColorLamp_Green = 23,  // DO3-8

		// DO4模块
		/// <summary>
		/// 蜂鸣器/单路继电器
		/// </summary>
		Buzzer = 24,  // DO4-1

		/// <summary>
		/// 推杆伸缩控制/单路继电器
		/// </summary>
		PushRodExtendControl = 25,  // DO4-2

		/// <summary>
		/// 备用/单路继电器
		/// </summary>
		Reserved_DO4_3 = 26,  // DO4-3

		/// <summary>
		/// 备用/单路继电器
		/// </summary>
		Reserved_DO4_4 = 27,  // DO4-4

		/// <summary>
		/// 备用/单路继电器
		/// </summary>
		Reserved_DO4_5 = 28,  // DO4-5

		/// <summary>
		/// 备用/单路继电器
		/// </summary>
		Reserved_DO4_6 = 29,  // DO4-6

		/// <summary>
		/// 备用/单路继电器
		/// </summary>
		Reserved_DO4_7 = 30,  // DO4-7

		/// <summary>
		/// 备用/单路继电器
		/// </summary>
		Reserved_DO4_8 = 31   // DO4-8
	}

	/// <summary>
	/// 模拟输入信号ID
	/// </summary>
	public enum AISignalId
	{
		/// <summary>
		/// 冷凝器压力
		/// </summary>
		CondenserPressure = 0,

		/// <summary>
		/// 加热温度
		/// </summary>
		HeatingTemperature = 1,

		/// <summary>
		/// 冷凝器温度
		/// </summary>
		CondenserTemperature = 2
	}

	/// <summary>
	/// IO信号工具类
	/// </summary>
	public static class IOSignalUtils
	{
		/// <summary>
		/// 获取DI信号ID字符串
		/// </summary>
		public static string GetDISignalName(DISignalId id)
		{
			int module = ((int)id / 8) + 1;
			int channel = ((int)id % 8) + 1;
			return $"DI{module}-{channel}";
		}

		/// <summary>
		/// 获取DO信号ID字符串
		/// </summary>
		public static string GetDOSignalName(DOSignalId id)
		{
			int module = ((int)id / 8) + 1;
			int channel = ((int)id % 8) + 1;
			return $"DO{module}-{channel}";
		}

		/// <summary>
		/// 获取AI信号ID字符串
		/// </summary>
		public static string GetAISignalName(AISignalId id) => $"AI_{id}";

		/// <summary>
		/// 解析DI信号ID
		/// </summary>
		public static bool TryParseDISignalId(string signalName, out DISignalId signalId)
		{
			signalId = DISignalId.EmergencyStop;

			if (string.IsNullOrEmpty(signalName) || !signalName.StartsWith("DI"))
				return false;

			try
			{
				string[] parts = signalName.Split('-');
				if (parts.Length != 2)
					return false;

				// 解析模块号和通道号
				int module = int.Parse(parts[0].Substring(2));
				int channel = int.Parse(parts[1]);

				// 计算枚举值
				int enumValue = (module - 1) * 8 + (channel - 1);

				if (Enum.IsDefined(typeof(DISignalId), enumValue))
				{
					signalId = (DISignalId)enumValue;
					return true;
				}

				return false;
			}
			catch
			{
				return false;
			}
		}

		/// <summary>
		/// 解析DO信号ID
		/// </summary>
		public static bool TryParseDOSignalId(string signalName, out DOSignalId signalId)
		{
			signalId = DOSignalId.CondenserAddLiquidValve1;

			if (string.IsNullOrEmpty(signalName) || !signalName.StartsWith("DO"))
				return false;

			try
			{
				string[] parts = signalName.Split('-');
				if (parts.Length != 2)
					return false;

				// 解析模块号和通道号
				int module = int.Parse(parts[0].Substring(2));
				int channel = int.Parse(parts[1]);

				// 计算枚举值
				int enumValue = (module - 1) * 8 + (channel - 1);

				if (Enum.IsDefined(typeof(DOSignalId), enumValue))
				{
					signalId = (DOSignalId)enumValue;
					return true;
				}

				return false;
			}
			catch
			{
				return false;
			}
		}

		/// <summary>
		/// 解析AI信号ID
		/// </summary>
		public static bool TryParseAISignalId(string signalName, out AISignalId signalId)
		{
			signalId = AISignalId.CondenserPressure;

			if (string.IsNullOrEmpty(signalName) || !signalName.StartsWith("AI_"))
				return false;

			string idPart = signalName.Substring(3);
			return Enum.TryParse(idPart, out signalId);
		}

		/// <summary>
		/// 获取DI信号位掩码
		/// </summary>
		public static int GetDIBitMask(DISignalId id)
		{
			int moduleIndex = (int)id / 8;
			int channelIndex = (int)id % 8;
			return 1 << (moduleIndex * 8 + channelIndex);
		}

		/// <summary>
		/// 获取DO信号位掩码
		/// </summary>
		public static int GetDOBitMask(DOSignalId id)
		{
			int moduleIndex = (int)id / 8;
			int channelIndex = (int)id % 8;
			return 1 << (moduleIndex * 8 + channelIndex);
		}
	}
}